package snake.demo.obj;

import snake.demo.common.GameWin;
import snake.demo.util.GameUtils;

import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.List;

/**
 * @author A
 * @version 1.0
 * @description: TODO
 * @date 2022/10/24 14:34
 */
public class HeadObj extends GameObj {


    //方向
    private String direction = "right";

    public String getDirection() {
        return direction;
    }

    public HeadObj(Image img, int x, int y, GameWin frame) {
        super(img, x, y, frame);
        //键盘监听事件
        this.frame.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                changeDir(e);
            }
        });
    }

    //控制移动方向 w-up a-left s-down d-right
    public void changeDir(KeyEvent e) {
        switch (e.getKeyCode()) {
            case KeyEvent.VK_LEFT:
                if (!"right".equals(direction)) {
                    direction = "left";
                    img = GameUtils.leftImg;
                }
                break;
            case KeyEvent.VK_DOWN:
                if (!"up".equals(direction)) {
                    direction = "down";
                    img = GameUtils.downImg;

                }
                break;
            case KeyEvent.VK_RIGHT:
                if (!"left".equals(direction)) {
                    direction = "right";
                    img = GameUtils.rightImg;
                }
                break;
            case KeyEvent.VK_UP:
                if (!"down".equals(direction)) {
                    direction = "up";
                    img = GameUtils.upImg;
                }
                break;
            default:
                break;
        }
    }

    //蛇的移动
    public void move() {
        //蛇身移动

        java.util.List<BodyObj> bodyObjList = this.frame.bodyObjList;
        for (int i = bodyObjList.size() - 1; i >= 1; i--) {
            bodyObjList.get(i).x = bodyObjList.get(i - 1).x;
            bodyObjList.get(i).y = bodyObjList.get(i - 1).y;
            //游戏失败,蛇身碰撞
            if (this.x == bodyObjList.get(i).x && this.y == bodyObjList.get(i).y) {
                GameWin.state = 3;
            }
        }
        try {
            bodyObjList.get(0).x = this.x;
            bodyObjList.get(0).y = this.y;
        } catch (Exception e) {
            e.printStackTrace();
        }
        //蛇头移动
        switch (direction) {
            case "up":
                y -= height;
                break;
            case "down":
                y += height;
                break;
            case "left":
                x -= width;
                break;
            case "right":
                x += width;
                break;
            default:
                break;
        }


    }

    @Override
    public void paintSelf(Graphics g) {
        super.paintSelf(g);
        Integer newX = null;
        Integer newY = null;
        //食物
        FoodObj food = this.frame.foodObj;
        //蛇吃食物
        if (this.x == food.x && this.y == food.y) {
            //将新的随机的food对象赋值给窗口类
            this.frame.foodObj = food.getFood();
            //获取蛇身最后一个元素
            BodyObj bodyObj = this.frame.bodyObjList.get(this.frame.bodyObjList.size() - 1);
            newX = bodyObj.x;
            newY = bodyObj.y;
            //分数+1
            this.frame.score++;
        }
        //游戏通关
        if (this.frame.score >= 3) {
            GameWin.state = 4;
        }
        move();
        if (newX != null && newY != null) {
            this.frame.bodyObjList.add(new BodyObj(GameUtils.bodyImg, newX, newY, this.frame));
        }


        //越界处理
        if (x < 0) {
            x = 570;
        } else if (x > 570) {
            x = 0;
        } else if (y < 30) {
            y = 570;
        } else if (y > 570) {
            y = 30;
        }
    }
}
